/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.groupb.circus.impl_interfaces;

import com.groupb.circus.interfaces.Listener;
import com.groupb.circus.interfaces.SearchProfile;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.Marshaller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromContainsFilter;
import org.jivesoftware.smack.filter.NotFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smack.packet.Presence;

/**
 *
 * @author bhavna
 */
public class InstantMessage implements com.groupb.circus.interfaces.InstantMessage {

    private static InstantMessage instantMessage = null;
    private static Log log = LogFactory.getLog(InstantMessage.class);
    private String login_name = "";
    private PacketFilter filter = null;
    private PacketListener hi = null;
    private MultiUserChat chat = null;
    private List<String> users = null;
    private XMPPConnection con = null;
    private Listener escucha;
   

    public InstantMessage() {
    }

    public synchronized static InstantMessage getInstance() {

        if (instantMessage == null) {
            instantMessage = new InstantMessage();

        }
        return instantMessage;
    }

    @Override
    public boolean login(String aLogin, String aPassword, String resource) {

 
        // get textfield's userna
        this.login_name = aLogin;
       
        try {
           
            ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
            config.setSASLAuthenticationEnabled(true);
            config.setReconnectionAllowed(true);
            con = new XMPPConnection(config);
            
            con.connect();
            con.login(aLogin, aPassword, resource);
           
          filter = new AndFilter(new PacketTypeFilter(Message.class), new NotFilter(new FromContainsFilter(con.getUser())));
            
         //filter = new AndFilter();
         con.createPacketCollector(filter);

            // initialise your chatgroup
         chat = new MultiUserChat(con, aLogin + "'s Chat");
         
            // initialise your message

         Roster roster = con.getRoster();
           
         for (Iterator<RosterEntry> i = roster.getEntries().iterator(); i.hasNext();) {
         RosterEntry re = (RosterEntry) i.next();
         users.add(re.getUser());
        }
         escucha = new com.groupb.circus.impl_interfaces.Listener();
    
         
          con.addPacketListener(escucha, filter);
          
           return true;
           
//            
            
        } catch (Throwable ex) {
            ex.printStackTrace();
            // any thing during login, disable user to send message
            return false;
        }

        
        // TODO: Actualizar roster con nuevos usuarios

    }

    public void logout() {
        if (con != null) {
            con.disconnect();
        }
    }

  /*  public ArrayList<RosterEntry> getContactosOnline() {

        ArrayList<RosterEntry> contactos = new ArrayList<RosterEntry>();
        Roster roster = con.getRoster();

        for (Iterator<RosterEntry> i = roster.getEntries().iterator(); i.hasNext();) {

            RosterEntry re = (RosterEntry) i.next();
            Presence p = roster.getPresence(re.getUser());

            String estado = p.getStatus();
            if (p.isAvailable()) {
                contactos.add(re);
            }
        }
        return contactos;
    }

    public void addListener(Listener listener, final PacketFilter filter) {

        con.createPacketCollector(filter);
        con.addPacketListener(listener, filter);
    }*/

   




    @Override
    public void getContent(String url, String user_url) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void addListener(Marshaller.Listener listener, XMPPConnection connection, PacketFilter filter) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void searchContent(SearchProfile search) {
        
        
        
        
        
    }



    
   
        
        
        
        
    
    
    
    
    
}
